package com.barbeque.mall.service.wx;

import com.barbeque.mall.bean.order.MarketOrder;
import com.barbeque.mall.bean.order.MarketOrderExample;
import com.barbeque.mall.bean.order.MarketOrderGoodsExample;
import com.barbeque.mall.bean.wx.user.OrderRespVo;
import com.barbeque.mall.bean.wx.user.WxUserIndexVo;
import com.barbeque.mall.mapper.MarketOrderGoodsMapper;
import com.barbeque.mall.mapper.MarketOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Tezel
 * @since 2022/07/21 14:11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WxUserServiceImpl implements WxUserService {

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Override
    public OrderRespVo index() {

        //统计未付款订单 状态码101
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andOrderStatusEqualTo(new Short("101"))
        .andDeletedEqualTo(false);

        Long unpaid = orderMapper.countByExample(orderExample);


        //统计待发货订单，状态码201

        MarketOrderExample orderExample1 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria1 = orderExample1.createCriteria();
        criteria1.andOrderStatusEqualTo(new Short("201"))
                .andDeletedEqualTo(false);


        Long unship = orderMapper.countByExample(orderExample1);


        //统计待收货订单，状态码301

        MarketOrderExample orderExample2 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria2 = orderExample2.createCriteria();
        criteria2.andOrderStatusEqualTo(new Short("301"))
                .andDeletedEqualTo(false);

        Long unrecv = orderMapper.countByExample(orderExample2);


        //统计待评论商品，状态码401

        MarketOrderExample orderExample3 = new MarketOrderExample();
        MarketOrderExample.Criteria criteria3 = orderExample3.createCriteria();
        criteria3.andOrderStatusEqualTo(new Short("401"))
        .andDeletedEqualTo(false);

        //查找订单中未评论商品
        List<MarketOrder> marketOrders = orderMapper.selectByExample(orderExample3);

        long unCommnet = 0;

        for (MarketOrder marketOrder : marketOrders) {
            //用orderid 去 ordergoods表找对应goods，找未评论的
            Integer orderId = marketOrder.getId();

            MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();

            MarketOrderGoodsExample.Criteria criteria4 = orderGoodsExample.createCriteria();

            criteria4.andOrderIdEqualTo(orderId)
            .andCommentEqualTo(0);

            long goodsUncomment = orderGoodsMapper.countByExample(orderGoodsExample);

            unCommnet += goodsUncomment;

        }

        Long uncomment = new Long(unCommnet);

        WxUserIndexVo wxUserIndexVo = new WxUserIndexVo();
        wxUserIndexVo.setUncomment(uncomment);
        wxUserIndexVo.setUnpaid(unpaid);
        wxUserIndexVo.setUnrecv(unrecv);
        wxUserIndexVo.setUnship(unship);

        OrderRespVo orderRespVo = new OrderRespVo();
        orderRespVo.setOrder(wxUserIndexVo);

        return orderRespVo;
    }
}
